<?php

namespace Admin\Controller;

/**
 * 考试控制器
 */
class ExamController extends AdminController{
    
    public function index(){
        $ExamInfo = D('ExamInfo');
        $where = array(
            'type' => array('GT', 0),
        );
        $exams = $ExamInfo->where($where)->select();
        
        $this->exams = $exams;
        $this->display();
    }
    
    public function addExam(){
        if(IS_POST){
            $ExamInfo = D('ExamInfo');
            
            $post = I("post.");
            $data = array(
                'name' => $post['name'],
                'description' => $post['description'],
                'start_time' => strtotime($post['start_time']),
                'end_time' => strtotime($post['end_time']),
                'time' => strtotime($post['time']),
                'people_num' => $post['people_num'],
                'pay_num' => $post['pay_num'],
                'type' => $post['type'],
            );            
            if($ExamInfo->create($data)){
                $ExamInfo->add($data);
                $return = array(
                    'status' => 1,
                    'info' => '添加考试【' . $data['name'] . '】成功',
                );
            }else{
                $return = array(
                    'status' => 0,
                    'info' => $ExamInfo->getError(),
                );
            };   
            $this->ajaxReturn($return);
        }  else {
            $this->display();
        }
    }
    
    public function editExam(){
        $ExamInfo = D('ExamInfo');
        
        if(IS_POST){
            $post = I("post.");
            $data = array(
                'id' => $post['id'],
                'name' => $post['name'],
                'description' => $post['description'],
                'start_time' => strtotime($post['start_time']),
                'end_time' => strtotime($post['end_time']),
                'time' => strtotime($post['time']),
                'people_num' => $post['people_num'],
                'pay_num' => $post['pay_num'],
                'type' => $post['type'],
            );            
            if($ExamInfo->create($data)){
                $ExamInfo->where('id='.$data['id'])->save($data);
                $return = array(
                    'status' => 1,
                    'info' => "添加考试【" . $data['name'] . "】成功",
                );
            }else{
                $return = array(
                    'status' => 0,
                    'info' => $ExamInfo->getError(),
                );
            }
            $this->ajaxReturn($return);
        }  else {
            $id = I('get.id');
            $exam = $ExamInfo->where(array('id'=>$id))->find();
            
            $this->exam = $exam;
            $this->display();            
        }

    }
    
    public function delExam(){
        if(!IS_POST){
            $this->error('页面不存在');
        }
        
        $ExamInfo = D('ExamInfo');
        
        $id = I('post.id');
        if($ExamInfo->where('id='.$id)->setField('type', 0) == 1){
            $return = array(
                'status' => 1,
                'info' => '删除成功',
            );
        }  else {
            $return = array(
                'status' => 0,
                'info' => '删除失败 ' . $ExamInfo->getError(),
            );
        }
        $this->ajaxReturn($return);
    }
    
    /*
     * 查看所有学院和专业
     */
    public function colg(){
        $Colg = D('College');
        
        $list = $Colg->order('id desc')->select();
        $tree = list_to_tree($list);
        
        $this->datas = $tree; 
        $this->display();
    }
    
    /**
     * 添加学院专业
     * @return boolean
     */
    public function addColg(){
        $Colg = D("College");
        
        if(IS_POST){
            $data = array(
                'name' => I('post.name'),
                'description' => I('post.description'),
                'pid' => I('post.pid'),
            );
            if($Colg->create($data)){
                $Colg->add($data);
                $return = array(
                    'status' => 1,
                    'info' => '添加【' . $data['name'] . '】学院成功',
                );
            }  else {
                $return = array(
                    'status' => 0,
                    'info' => $Colg->getError(),
                );
            }
            $this->ajaxReturn($return);
        }  else {
            $pid = I('get.pid');
            if(!empty($pid)){
                $res = $Colg->where('id='.$pid)->field('name,pid')->find();
                if($res['pid'] != 0){
                    $this->message = "非学院不能添加专业";
                    $this->display('Error/error');
                    return true;
                }
                
                $this->pid = $pid;
                $this->title = '【' . $res['name'] . '】添加专业信息';
                $this->name = '专业';
            }  else {
                $this->name = '学院';
                $this->title= '添加学院';                
            }

            $this->display();
        }
    }
    /**
     * 编辑学院专业
     */
    public function editColg(){
        $Colg = D('College');
        
        if(IS_POST){
            $data = I('post');
            if($Colg->create($data)){
                $Colg->save($data);
                $return = array(
                    'status' => 1,
                    'info' => '修改成功',
                );
            }  else {
                $return = array(
                    'status' => 0,
                    'info' => '修改失败 ' . $Colg->getError(),
                );
            }
            
            $this->ajaxReturn($return);
        }  else {
            $id = I('get.id');
            if(!empty($id)){
                $res = $Colg->where('id='.$id)->find();
                if(empty($res)){
                    $this->display('Error/error404');
                }
                $name = '专业';
                if($res['id'] == 0){
                    $name = '学院';
                }
                
                $this->data = $res;
                $this->id = $id;
                $this->name = $name;
                $this->title = '修改【' . $res['name'] . '】的详细信息';
                $this->display();
            }  else {
                $this->display('Error/error404');
            }
        }
    }
    
    /**
     * 删除学院专业
     * @return boolean
     */
    public function delColg(){
        if(!IS_POST){
            $this->display('Error/error404');
            return true;
        }
        $Colg = D('College');
        $id = I('post.id');
        //检查次id是否存在子id,若存在则不能删除
        $lists = $Colg->where('pid='.$id)->select();
        if(!empty($lists)){
            $return = array(
                'ststus' => 0,
                'info' => '此学院还有专业类别，请先删除此学院的子专业后再删除'
            );
        }else{
            if($Colg->where('id='.$id)->delete()){
                $return = array(
                    'status' => 1,
                    'info' => '删除成功'
                );
            }else{
                $return = array(
                    'status' => 0,
                    'info' => '删除失败' . $Colg->getError(),
                );
            }
        }
        $this->ajaxReturn($return);
    }
    
    /**
     * 查看可报名的考试
     */
    public function enterExam(){
        
        //查找所有可报名的考试
        $ExamInfo = D('ExamInfo');
        $where = array(
            'type' => array('GT', 0),
            'start_time' => array('ELT', time()),
            'end_time'=> array('EGT', time()),
        );
        $exams = $ExamInfo->where($where)->select();
        //查找可报名考试的已报名人数
        $Submit = D('Submit');
        foreach ($exams as $k => $v){
            $map = array(
                'eid' => $v['id'],
                'status' => array('GT', 0)
            );
            $num = $Submit->where($map)->count();
            $exams[$k]['submit_num'] = $num;
            $exams[$k]['deadline'] = timediff(time(), $v['end_time']);
        }
        
        $this->data = $exams;
        $this->display();
    }
    
    /**
     * 添加报名表
     */
    public function entryForm(){
        $Submit = D('Submit');
        $StudentInfo = D('StudentInfo');
        
        $Colg = D('College');
        $list = $Colg->order('id desc')->select();
        $tree = list_to_tree($list);
        
        if(IS_POST){
            p(I());
        } else {
            
            $this->display();
        }
    }
}